/****************************************************************************
 * 2025.5 GRAVEROBBER-PC
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvival
{
    public partial class LevelUpPanel : UIElement, IController
    {
        private LevelUpSystem lvUpSystem;
        private ResLoader loader = ResLoader.Allocate();
        
        private void Awake()
        {
            lvUpSystem = this.GetSystem<LevelUpSystem>();
            var iconAtlas = loader.LoadSync<SpriteAtlas>("Icon");
            foreach (var levelUpItem in lvUpSystem.LevelUpItems)
            {
                var itemCatch = levelUpItem;
                LevelUpTemplate.InstantiateWithParent(LevelUpRoot)
                    .Self(self =>
                    {
                        self.transform.Find("Icon").GetComponent<Image>().sprite =
                            iconAtlas.GetSprite(itemCatch.IconName);
                        var selftCatch = self;
                        self.onClick.AddListener(() =>
                        {
                            itemCatch.Upgrade();
                            AudioKit.PlaySound("AbilityLevelUp");
                            Time.timeScale = 1f;
                            this.Hide();
                        });
                        itemCatch.currentLevel.RegisterWithInitValue(lv =>
                        {
                            selftCatch.GetComponentInChildren<Text>().text = $"{itemCatch.Description}";
                        }).UnRegisterWhenGameObjectDestroyed(selftCatch);
                        selftCatch.Hide();
                        itemCatch.Visible.Register((visible) =>
                        {
                            if (visible)
                            {
                                selftCatch.Show();
                                if (lvUpSystem.Pairs.TryGetValue(itemCatch.Key, out var pairedName))
                                {
                                    var pairedItem = lvUpSystem.Dictionary[pairedName];
                                    if (pairedItem.currentLevel.Value > 0 && itemCatch.currentLevel.Value == 0)
                                    {
                                        var pairedNameText = selftCatch.transform.Find("PairedUpgradeNameText")
                                            .GetComponent<Text>();
                                        pairedNameText.text = "配对技能：" + pairedItem.Name;
                                        pairedNameText.Show();
                                        pairedNameText.transform.Find("Icon").GetComponent<Image>().sprite =
                                            iconAtlas.GetSprite(pairedItem.IconName);
                                    }
                                    else
                                    {
                                        selftCatch.transform.Find("PairedUpgradeNameText").GetComponent<Text>().Hide();
                                    }
                                }
                                else
                                {
                                    selftCatch.transform.Find("PairedUpgradeNameText").GetComponent<Text>().Hide();
                                }
                            }
                            else
                            {
                                selftCatch.Hide();
                            }
                        }).UnRegisterWhenGameObjectDestroyed(selftCatch);
                        itemCatch.currentLevel.RegisterWithInitValue(lv =>
                        {
                            selftCatch.GetComponentInChildren<Text>().text = $"{itemCatch.Description}";
                        }).UnRegisterWhenGameObjectDestroyed(selftCatch);
                    });
            }
        }
        protected override void OnBeforeDestroy()
        {
            loader.Recycle2Cache();
            loader = null;
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }
    }
}